import Vue from 'vue';

Vue.filter('normalTime', (time = '0', currencyType = '') => {
  if (time) {
    const odate = new Date();
    odate.setTime(time);
    const year = odate.getFullYear();
    const month = odate.getMonth() + 1;
    const day = odate.getDate();
    const hours = odate.getHours();
    const minutes = odate.getMinutes();
    const seconds = odate.getSeconds();
    let retime = null;
    if (currencyType.format) {
      const format = currencyType.format.split('-');
      for (let i = 0; i < format.length; i++) {
        if (format[i] === '' || typeof format[i] === 'undefined') {
          format.splice(i, 1);
          i -= 1;
        }
      }
      format.forecah((rec) => {
        if (rec === 'y') {
          !retime ? (retime = year) : (retime = null);
        }
        if (rec === 'M') {
          retime ? (retime += currencyType.type + month) : (retime = month);
        }
        if (rec === 'd') {
          retime ? (retime += currencyType.type + day) : (retime = day);
        }
        if (rec === 'h') {
          retime ? (retime += `  ${hours}`) : (retime = hours);
        }
        if (rec === 'm') {
          retime ? (retime += currencyType.type + minutes) : (retime = minutes);
        }
        if (rec === 's') {
          retime ? (retime += currencyType.type + seconds) : (retime = seconds);
        }
      });
    } else if (currencyType.type) {
      retime = `${year +
        currencyType.type +
        month +
        currencyType.type +
        day}  ${hours}${currencyType.type}${minutes}`;
    } else {
      retime = `${year}-${month}-${day}  ${hours}-${minutes}`;
    }
    return retime;
  }
});
